﻿// Copyright 2008 William P. Hickey Jr. and William U. Walker
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//     
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AgwpePort.Aprs;

namespace CSReporter
{
    public partial class EditStationsDialog : Form
    {
        private ConfigList configure;
        private StationList stations;

        #region " Constructor and Load event handler "
        public EditStationsDialog()
        {
            InitializeComponent();
            // Get the ConfigList object.
            configure = ConfigList.GetConfigList();
            // Get the StationList object.
            stations = StationList.GetStationList();
            // Reread the stations.xml file.
            stations.ReadXml(configure[0].DataDir + @"\stations.xml");
        }

        private void EditStationsDialog_Load(object sender, EventArgs e)
        {
            DataGridViewComboBoxColumn dirCol = new DataGridViewComboBoxColumn();

            dirCol.Items.Add("");
            dirCol.Items.Add("N");
            dirCol.Items.Add("E");
            dirCol.Items.Add("S");
            dirCol.Items.Add("W");
            dirCol.Name = "StreetDirection";

            TbStationDataGridView.DataSource = stations;

            TbStationDataGridView.Columns.Remove("StreetDirection");
            TbStationDataGridView.Columns.Add(dirCol);

            TbStationDataGridView.Columns["CallSign"].HeaderText = "Call Sign";
            TbStationDataGridView.Columns["CallSign"].DisplayIndex = 0;
            TbStationDataGridView.Columns["StreetNumber"].HeaderText = "Street Number";
            TbStationDataGridView.Columns["StreetNumber"].DisplayIndex = 1;
            TbStationDataGridView.Columns["StreetDirection"].HeaderText = "Direction";
            TbStationDataGridView.Columns["StreetDirection"].DisplayIndex = 2;
            TbStationDataGridView.Columns["StreetDirection"].DataPropertyName = "StreetDirection";
            TbStationDataGridView.Columns["StreetName"].HeaderText = "Street Name";
            TbStationDataGridView.Columns["StreetName"].DisplayIndex = 3;
            TbStationDataGridView.Columns["CityCode"].HeaderText = "City Code";
            TbStationDataGridView.Columns["CityCode"].DisplayIndex = 4;
            TbStationDataGridView.Columns["StateCode"].HeaderText = "State Code";
            TbStationDataGridView.Columns["StateCode"].DisplayIndex = 5;
            TbStationDataGridView.Columns["Crossroad1"].HeaderText = "Crossroad 1";
            TbStationDataGridView.Columns["Crossroad1"].DisplayIndex = 6;
            TbStationDataGridView.Columns["Crossroad2"].HeaderText = "Crossroad 2";
            TbStationDataGridView.Columns["Crossroad2"].DisplayIndex = 7;
            TbStationDataGridView.Columns["Latitude"].HeaderText = "Latitude";
            TbStationDataGridView.Columns["Latitude"].DisplayIndex = 8;
            TbStationDataGridView.Columns["Longitude"].HeaderText = "Longitude";
            TbStationDataGridView.Columns["Longitude"].DisplayIndex = 9;
        }
        #endregion
        #region " Additional methods and handlers "
        private void OK_Button_Click(object sender, System.EventArgs e)
        {
            stations.WriteXml(configure[0].DataDir + @"\stations.xml");
            DialogResult = DialogResult.OK;
        }

        private void Cancel_Button_Click(object sender, System.EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void DataGridView_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == TbStationDataGridView.Rows[e.RowIndex].Cells["CallSign"].ColumnIndex |
                e.ColumnIndex == TbStationDataGridView.Rows[e.RowIndex].Cells["StreetNumber"].ColumnIndex |
                e.ColumnIndex == TbStationDataGridView.Rows[e.RowIndex].Cells["StreetName"].ColumnIndex |
                e.ColumnIndex == TbStationDataGridView.Rows[e.RowIndex].Cells["CityCode"].ColumnIndex |
                e.ColumnIndex == TbStationDataGridView.Rows[e.RowIndex].Cells["StateCode"].ColumnIndex |
                e.ColumnIndex == TbStationDataGridView.Rows[e.RowIndex].Cells["Crossroad1"].ColumnIndex |
                e.ColumnIndex == TbStationDataGridView.Rows[e.RowIndex].Cells["Crossroad2"].ColumnIndex)
            {
                TbStationDataGridView.CurrentCell.Value = TbStationDataGridView.CurrentCell.Value.ToString().ToUpper();
            }
            else if (e.ColumnIndex == TbStationDataGridView.Rows[e.RowIndex].Cells["Latitude"].ColumnIndex |
                e.ColumnIndex == TbStationDataGridView.Rows[e.RowIndex].Cells["Longitude"].ColumnIndex)
            {
                Coordinate coord = new Coordinate(TbStationDataGridView.CurrentCell.Value.ToString());

                if (Convert.ToDouble(coord.ToDegrees()) == 0)
                    if (TbStationDataGridView.CurrentCell.Value.ToString().Trim().Length > 0)
                        MessageBox.Show("Invalid coordinate format.\nValid formats are:\nTwo digit degrees\n-or-\nDecimal degrees\n-or-\nDegrees and decimal minutes\nExamples:\n32 or 32.1990833 or 32 11.945", "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
    }
}
